package review.example;

public class JosePhus {
    int p = 41;
    int[] pArr = new int[p];

    {

    }

    public void josePhus1(int m) {
        for (int i = 1; i <= p; i++) {
            pArr[i - 1] = i;
        }
        int index = 0;
        int count = 0;
        while (p > 1) {
            count++;
            if (m == count) {
                System.out.println("第" + (pArr[index]) + "人被杀");
                for (int i = index; i < p - 1; i++) {
                    pArr[i] = pArr[i + 1];
                }
                p--;
                count = 0;
                index--;
            }
            index++;
            if (index == p) {
                index = 0;
            }
        }

    }

    public void josePhus2(int m) {
        for (int i = 1; i <= p; i++) {
            pArr[i - 1] = 0;
        }
        int out = 0;
        int i = 0;
        int count = 0;
        int num = 0;
        while (out < p) {
            if (pArr[i] == 0) {
                count++;
                if (count == m) {
                    System.out.println("第" + (++num) + "次，第" + (i + 1) + "人被杀");
                    pArr[i] = 1;
                    count = 0;
                    out++;
                }

            }
            i++;
            if (i == p) {
                i = 0;
            }
        }
    }

    public static void main(String[] args) {
        JosePhus j = new JosePhus();
        j.josePhus2(3);
    }
}
